package resx

import (
	"dealcenter/utils/logx"
	"github.com/gin-gonic/gin"
	"github.com/pkg/errors"
)

// 响应模版
type Response struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

// 列表数据模版
type DataList struct {
	Items    interface{} `json:"item"`
	Total    int         `json:"total"`
	LastPage int64       `json:"lastPage"`
}

// 响应制作入口
func GinResp(msgCode int, data interface{}) *Response {
	if msgCode == SystemErr {
		err, ok := data.(error)
		if !ok {
			logx.LogrusObj.Warningln("[API] got a system error, but can't log that")
		} else {
			logx.LogrusObj.Warningln(errors.Wrap(err, "[API] got a system error"))
		}
		// 静止错误外漏
		return &Response{
			Code: SystemErr,
			Msg:  GetMsg(SystemErr),
			Data: nil,
		}
	}
	return &Response{
		Code: msgCode,
		Msg:  GetMsg(msgCode),
		Data: data,
	}
}
func GinRespH(msgCode int, data interface{}) *gin.H {
	if msgCode == SystemErr {
		err, ok := data.(error)
		if !ok {
			logx.LogrusObj.Warningln("[API] got a system error, but can't log that")
		}
		logx.LogrusObj.Warningln(errors.Wrap(err, "[API] got a system error"))
		// 静止错误外漏
		return &gin.H{
			"code": SystemErr,
			"msg":  GetMsg(SystemErr),
			"data": data,
		}
	}
	return &gin.H{
		"code": msgCode,
		"msg":  GetMsg(msgCode),
		"data": data,
	}
}
